package nm.aleksey.server;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import nm.aleksey.domain.Author;
//import nm.aleksey.domain.Book;

import com.google.web.bindery.requestfactory.shared.Locator;

public class AuthorLocator extends Locator<Author, Long> {
  public static final EntityManager entityManager() {
    return EMF.getInstance().get().createEntityManager();
  }

//  public void addBook(Author author, Book book) {
//
//    EntityManager em = entityManager();
//    try {
//      Author attachedAuthor = em.find(Author.class, author.getId());
//      attachedAuthor.getBooks().add(book);
//      em.merge(attachedAuthor);
//      em.close();
//    } catch (Exception e) {
//      e.printStackTrace();
//    } finally {
//    }
//  }

  @Override
  public Author create(Class<? extends Author> clazz) {
    try {
      return clazz.newInstance();
    } catch (InstantiationException e) {
      throw new RuntimeException(e);
    } catch (IllegalAccessException e) {
      throw new RuntimeException(e);
    }
  }

  public void delete(Author author) {
    EntityManager em = entityManager();
    try {
      Author attached = em.find(Author.class, author.getId());
      em.remove(attached);
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      em.close();
    }
  }

  @Override
  public Author find(Class<? extends Author> clazz, Long id) {
    if (id == null) {
      return null;
    }
    EntityManager em = entityManager();
    try {
      Author author = em.find(Author.class, id);
      return author;
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    } finally {
      em.close();
    }
  }

  public Author find(Long id) {
    Author author = find(Author.class, id);
    return author;
  }

  public List<Author> findAll() {
    EntityManager em = entityManager();
    try {
      Query q = em.createQuery("select a from Author a");
      return q.getResultList();
    } catch (Exception e) {
      e.printStackTrace();
      return null;
    } finally {
      em.close();
    }
  }

//  public List<Book> getBookList(Author author) {
//    return getBookList(author.getId());
//  }
//
//  public List<Book> getBookList(Long id) {
//    EntityManager em = entityManager();
//    try {
//      Author attached = em.find(Author.class, id);
//      return attached.getBooks();
//    } catch (Exception e) {
//      e.printStackTrace();
//      return null;
//    } finally {
//      em.close();
//    }
//  }

  @Override
  public Class<Author> getDomainType() {
    return Author.class;
  }

  @Override
  public Long getId(Author domainObject) {
    return domainObject.getId();
  }

  @Override
  public Class<Long> getIdType() {
    return Long.class;
  }

  @Override
  public Object getVersion(Author domainObject) {
    return domainObject.getVersion();
  }

  public void save(Author author) {
    EntityManager em = entityManager();
    try {
      em.merge(author);
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      em.close();
    }
  }
}
